package com.aidong.pay.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.aidong.coupons.biz.MstCouponsBiz;
import com.aidong.discount.biz.MstDiscountBiz;
import com.aidong.pay.biz.OrderManagementBiz;
import com.aidong.pay.biz.OrdergeneratedBiz;
import com.aidong.pay.config.AlipayConfig;
import com.aidong.pay.config.WeChatConfig;
import com.aidong.pay.entity.TrnCampaignSignOrder;
import com.aidong.util.bus.SecurityUtils;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class PayConfigurationInfoAction implements ModelDriven<TrnCampaignSignOrder> {

	private TrnCampaignSignOrder trnCampaignSignOrder;

	private String ticketNum;

	private OrdergeneratedBiz ordergeneratedBiz;

	private OrderManagementBiz orderManagementBiz;

	private MstCouponsBiz mstCouponsBiz;

	private MstDiscountBiz mstDiscountBiz;// 折扣券service

	@Autowired
	public void setOrdergeneratedBiz(OrdergeneratedBiz ordergeneratedBiz) {
		this.ordergeneratedBiz = ordergeneratedBiz;
	}

	@Autowired
	public void setOrderManagementBiz(OrderManagementBiz orderManagementBiz) {
		this.orderManagementBiz = orderManagementBiz;
	}

	@Autowired
	public void setMstCouponsBiz(MstCouponsBiz mstCouponsBiz) {
		this.mstCouponsBiz = mstCouponsBiz;
	}

	@Autowired
	public void setMstDiscountBiz(MstDiscountBiz mstDiscountBiz) {
		this.mstDiscountBiz = mstDiscountBiz;
	}

	public void setTicketNum(String ticketNum) {
		this.ticketNum = ticketNum;
	}

	@Override
	public TrnCampaignSignOrder getModel() {
		if (trnCampaignSignOrder == null) {
			trnCampaignSignOrder = new TrnCampaignSignOrder();
		}
		return trnCampaignSignOrder;
	}

	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	@Action(value = "getpayconfigmsg", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	@Transactional
	public String getPayConfigInfo() throws Exception {
		int payType = trnCampaignSignOrder.getPayType(); // 支付方式
		int orderId = trnCampaignSignOrder.getOrderId(); // 订单ID
		int memorcom = trnCampaignSignOrder.getMemorcom(); // 是否会员
		int pickUpWay = trnCampaignSignOrder.getPickUpWay();// 取货方式
		double freight = trnCampaignSignOrder.getFreight();// 快递费
		if (pickUpWay != 2) 
			freight = 0.00;
		map = new HashMap<String, Object>();
		System.out.println("支付类型："+payType+";码："+ticketNum+"订单："+orderId);
		Map<String, Object> configMap = new HashMap<String, Object>();
		if (payType > 0 && orderId > 0) {
			if (payType == 1) { // 支付宝
				configMap.put("partner", SecurityUtils.encryptPartner(AlipayConfig.PARTNER));
				configMap.put("seller", SecurityUtils.encryptSeller(AlipayConfig.SELLER));
				configMap.put("private_key", AlipayConfig.RSA_PRIVATE);
			} else if (payType == 2) { // 微信
				configMap.put("appkey", SecurityUtils.encryptPartner(WeChatConfig.APP_KEY));
				configMap.put("mchId", SecurityUtils.encryptPartner(WeChatConfig.MCH_ID));
			} else if (payType == 5 || payType == 6) {
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("couponsNum", ticketNum);
				paraMap.put("orderId", orderId);
				// 将订单ID填入到代金券表，并设置为已使用--存储过程
				orderManagementBiz.editCouponsInfoOrderId(paraMap);
				if ((Integer) paraMap.get("updateResult") == 0) {
					map.put("code", 0);
					map.put("msg", "代金券无效");
					return "succ";
				}

				if (payType == 5) {
					// 支付宝
					configMap.put("partner", SecurityUtils.encryptPartner(AlipayConfig.PARTNER));
					configMap.put("seller", SecurityUtils.encryptSeller(AlipayConfig.SELLER));
					configMap.put("private_key", AlipayConfig.RSA_PRIVATE);
				} else if (payType == 6) {
					// 微信
					configMap.put("appkey", SecurityUtils.encryptPartner(WeChatConfig.APP_KEY));
					configMap.put("mchId", SecurityUtils.encryptPartner(WeChatConfig.MCH_ID));
				}
			} else if (payType == 7 || payType == 8) {
				// 折扣券
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("discountNum", ticketNum);
				paraMap.put("orderId", orderId);

				// 将订单ID填入到折扣券表，并设置为已使用--存储过程
				orderManagementBiz.editDiscountInfoOrderId(paraMap);
				if ((Integer) paraMap.get("updateResult") == 0) {
					map.put("code", 0);
					map.put("msg", "折扣券无效");
					return "succ";
				}

				if (payType == 7) {
					// 支付宝
					configMap.put("partner", SecurityUtils.encryptPartner(AlipayConfig.PARTNER));
					configMap.put("seller", SecurityUtils.encryptSeller(AlipayConfig.SELLER));
					configMap.put("private_key", AlipayConfig.RSA_PRIVATE);
				} else if (payType == 8) {
					// 微信
					configMap.put("appkey", SecurityUtils.encryptPartner(WeChatConfig.APP_KEY));
					configMap.put("mchId", SecurityUtils.encryptPartner(WeChatConfig.MCH_ID));
				}
			} else if (payType == 9 || payType == 10) {
				// 兑换券
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("exchangeTicketNum", ticketNum);
				paraMap.put("orderId", orderId);

				orderManagementBiz.editExchangeTicketsInfo(paraMap);
				if ((Integer) paraMap.get("updateResult") == 0) {
					map.put("code", 0);
					map.put("msg", "兑换券无效");
					return "succ";
				}

				if (payType == 9) {
					// 支付宝
					configMap.put("partner", SecurityUtils.encryptPartner(AlipayConfig.PARTNER));
					configMap.put("seller", SecurityUtils.encryptSeller(AlipayConfig.SELLER));
					configMap.put("private_key", AlipayConfig.RSA_PRIVATE);
				} else if (payType == 10) {
					// 微信
					configMap.put("appkey", SecurityUtils.encryptPartner(WeChatConfig.APP_KEY));
					configMap.put("mchId", SecurityUtils.encryptPartner(WeChatConfig.MCH_ID));
				}
			}
		} else {
			System.out.println("参数缺失");
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
		Map<String, Object> orderMap = new HashMap<String, Object>();
		orderMap = ordergeneratedBiz.getPayOrderDispose(orderId);
		double amount = Double.parseDouble(orderMap.get("amount").toString());
		double goodsActually = 0.00;
		try {
			// 修改支付订单支付类型
			Map<String, Object> paraMap = new HashMap<String, Object>();
			if (payType == 5 || payType == 6) {// 代金券+支付宝，微信+支付宝

				paraMap.put("couponsNum", ticketNum.toUpperCase());
				Map<String, Object> resultMap = new HashMap<String, Object>();
				resultMap = mstCouponsBiz.getCouponsInfo(paraMap);
				double priceCoupons = Double.parseDouble(resultMap.get("couponsPrice").toString());
				double privilege = 0.00, actually = 0.00;
				if (priceCoupons >= amount) {// 计算优惠金额
					// privilege = amount;
					// goodsActually = 0.00;
					if (pickUpWay == 2) {// 快递
						actually = freight;
					}
				} else {
					goodsActually = amount - priceCoupons;
					actually = goodsActually;
					if (pickUpWay == 2) {// 快递
						actually = goodsActually + freight;
					}
				}
				privilege = priceCoupons;// 优惠金额 存储券的价格
				paraMap.put("privilege", privilege);
				paraMap.put("actually", actually);
				paraMap.put("freight", freight);
				paraMap.put("goodsActually", goodsActually);
				paraMap.put("payType", payType);
				paraMap.put("memorcom", memorcom);
				paraMap.put("orderId", orderId);
				ordergeneratedBiz.editorderPayTypeCoupons(paraMap);

			} else if (payType == 7 || payType == 8) {// 折扣券+支付宝，折扣券+微信
				paraMap.put("discountNum", ticketNum.toUpperCase());
				Map<String, Object> resultMap = new HashMap<String, Object>();
				resultMap = mstDiscountBiz.getDiscountInfo(paraMap);
				double discount = Double.parseDouble(resultMap.get("discountValue").toString()) * 0.1;
				double discountAc = 1 - discount;
				double actually = 0.00;
				goodsActually = amount * discount;
				actually = goodsActually;
				if (pickUpWay == 2) {// 快递
					actually = goodsActually + freight;
				}
				paraMap.put("discountAc", discountAc);
				paraMap.put("actually", actually);
				paraMap.put("goodsActually", goodsActually);
				paraMap.put("orderId", orderId);
				paraMap.put("payType", payType);
				paraMap.put("memorcom", memorcom);
				paraMap.put("freight", freight);
				ordergeneratedBiz.editorderPayTypeDiscount(paraMap);
			} else if (payType == 9 || payType == 10) {// 兑换券+支付宝，兑换券+微信
				// 用了兑换券还需要付钱，那么一定是快递的方式
				paraMap.put("orderId", orderId);
				paraMap.put("freight", freight);
				paraMap.put("payType", payType);
				paraMap.put("memorcom", memorcom);
				ordergeneratedBiz.editorderPayTypeExchanges(paraMap);
			} else {// 支付宝或者微信
				double actually = amount;
				if (pickUpWay == 2) {
					actually = actually + freight;
				}
				paraMap.put("actually", actually);
				paraMap.put("freight", freight);
				paraMap.put("payType", payType);
				paraMap.put("orderId", orderId);
				paraMap.put("memorcom", memorcom);
				paraMap.put("couponsNum", ticketNum);
				ordergeneratedBiz.editorderPayType(paraMap);
			}

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}

		map.put("code", 1);
		map.put("configMsg", configMap);
		return "succ";
	}
}
